/***************************************************************************
 *   Copyright (C) 2008 by Eduardo Gurgel Pinho                            *
 *   edgurgel@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#include "qtimage.h"

#include <QImage>
#include <QColor>

#include <string>


namespace Ui {

Qtimage::Qtimage(const unsigned int _width,const unsigned int _height) : Sirat::Common::Image(_width,_height) {
	image = new QImage(_width,_height,QImage::Format_RGB32);

}

Qtimage::Qtimage(void) : Sirat::Common::Image() , image(0) { }

Qtimage::Qtimage(const Qtimage& _qtimage) : Sirat::Common::Image(_qtimage) {
	image = new QImage(*(_qtimage.image));
}

Qtimage::~Qtimage(void) {
	delete image;
}

void Qtimage::save_file(const char* _file) {
	for(unsigned int x = 0;x < width;x++) {
		for(unsigned int y = 0; y < height;y++) {
			Sirat::Common::Color color = pixels[x][y];
			double r = color.red;
			double g = color.green;
			double b = color.blue;
			unsigned int py,px;
			py = height - y;	
			px = x;
			if (px > width-1) px = width-1;
	
			if (py > height-1) py = height-1;
	
			int ir = (int) (r * 255.0);
			int ig = (int) (g * 255.0);
			int ib = (int) (b * 255.0);

			ir = std::max(0,std::min(ir,255));
			ig = std::max(0,std::min(ig,255));		
			ib = std::max(0,std::min(ib,255));

			QRgb qcolor = qRgb(ir,ig,ib);
			image->setPixel(px,py,qcolor);
		}
	}
	image->save(_file);
	filename = std::string(_file);
}

void Qtimage::load_file(const char* _file) {
	if(image)
		delete image;
		 
	image = new QImage(_file);
	
	Sirat::Common::Image::initiate_pixels(width,height);
	
	for(unsigned int x = 0;x < width;x++) {
		for(unsigned int y = 0; y < height;y++) {
			QColor color(image->pixel(x,y));
			pixels[x][y] = Sirat::Common::Color(color.red()/255.0,color.green()/255.0,color.blue()/255.0);
		}
	}
	filename = std::string(_file);
	load = false;
}

QImage* Qtimage::get_QImage(void) const {
	for(unsigned int x = 0;x < width;x++) {
		for(unsigned int y = 0; y < height;y++) {
			Sirat::Common::Color color = pixels[x][y];
			double r = color.red;
			double g = color.green;
			double b = color.blue;
			unsigned int py,px;
			py = height - y;	
			px = x;
			if (px > width-1) px = width-1;
	
			if (py > height-1) py = height-1;
	
			int ir = (int) (r * 255.0);
			int ig = (int) (g * 255.0);
			int ib = (int) (b * 255.0);

			ir = std::max(0,std::min(ir,255));
			ig = std::max(0,std::min(ig,255));		
			ib = std::max(0,std::min(ib,255));

			QRgb qcolor = qRgb(ir,ig,ib);
			image->setPixel(px,py,qcolor);
		}
	}
	return image;
}

Qtimage& Qtimage::operator=(const Qtimage& _qtimage) {
	if(this != &_qtimage) {
		Image::operator=(_qtimage);
		image = new QImage(*(_qtimage.image));
	}
	return *this;
}

Qtimage* Qtimage::operator+(const Qtimage* const _Qtimage) const {
	Qtimage* result = new Qtimage(height,width);
		
	Sirat::Common::Color** result_pixels = Sirat::Common::Image::operator+(_Qtimage);
	
	for(unsigned int x = 0;x < width; x++)
		for (unsigned int y = 0; y < height; y++)
			result->pixels[x][y] = result_pixels[x][y];
	return result;
}

}

